package au.com.langdale.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * This utility gathers together the hibernate operations we are using.
 * 
 * @author adv
 *
 */
public class Sessions {
	private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * The session factory is created at class load time and is 
     * provided here.
     * 
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    /**
     * All hibernate operations take place within a session.
     * The session for the current thread is provided here.
     */
    public static Session getCurrentSession() {
    	return sessionFactory.getCurrentSession();
    }
    
    /**
     * Start a transaction.
     *
     */
    public static void begin() {
    	getCurrentSession().beginTransaction();
    }
    
    /**
     * Commit the transaction.
     *
     */
    public static void commit() {
    	getCurrentSession().getTransaction().commit();
    }
    
    /**
     * Rollback the transaction.
     * 
     */
    public static void rollback() {
    	getCurrentSession().getTransaction().rollback();
    }

    /**
     * Execute an HQL query returning one page of the results.
     */
    public static List query(String expr, int first, int quota) {
    	Query query = getCurrentSession().createQuery(expr);
    	query.setFirstResult(first);
    	query.setMaxResults(quota);
		return query.list();
    }
    
    /**
     * Execute an HQL query with a parameter returning one page of the results.
     */
    public static List query(String expr, String p1, int first, int quota) {
    	Query query = getCurrentSession().createQuery(expr);
    	query.setString(0, p1);
    	query.setFirstResult(first);
    	query.setMaxResults(quota);
		return query.list();
    }
    
    /**
     * Execute an HQL query with parameters returning one page of the results.
     */
    public static List query(String expr, String p1, String p2, int first, int quota) {
    	Query query = getCurrentSession().createQuery(expr);
    	query.setString(0, p1);
    	query.setString(1, p2);
    	query.setFirstResult(first);
    	query.setMaxResults(quota);
		return query.list();
    }
    
    /**
     * Execute an HQL query with a parameter returning all results.
     */
    public static List query(String expr, String p1) {
    	return getCurrentSession().createQuery(expr).setString(0, p1).list();
    }
    
    public static void save(Object record) {
    	getCurrentSession().save(record);
    }

	public static void merge(Object record) {
		getCurrentSession().merge(record);		
	}

	public static void delete(Object record) {
		getCurrentSession().delete(record);
	}
}
